<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class RequestServicesTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addTests($tests)
    {
		$type="";
		$service_names="";
		$user_id="";
		$phone="";
		$alterPhone="";
		$landmark="";
		$address="";
		$pincode="";
		$selectinsurence="";
		$insurance="";
		$polnumber="";
		$polholname="";
		$dependent="";
		if(count($tests)!=0){
			if($tests['type']==1){
				$service_names.="dressing".'***';
			}else{
				foreach($tests['hidtest_name_price'] as $key=>$value){
					$service_names.=$value.'***';
				}
			}
		}
		if($tests['type']==1){
			$data = array(
			'user_id' 			=> $tests['user_id'], 			
			'service_names'  	=> rtrim($service_names,'***'),  	
			'dressname'  		=>$tests['dressname'], 
			'dressdescription'  =>$tests['dressdes'], 	
			'phone' 		 	=>'',   
			'alterphone'	 	=>'', 				
			'landmark'	 		=>'', 				
			'address'	 		=>'',  				
			'pincode'	 		=>'', 				
			'selectinsurence'	=>'', 				
			'insurance_id'	 	=>'', 				
			'policynumber'	 	=>'', 				
			'policyholdername'	=>'', 				
			'ustdependent'	 	=>'', 				
			'status'	 		=> 0,
			'added_date' 		=> date('Y-m-d H:i:s'),
			'updated_date' 		=> date('Y-m-d H:i:s'),
			);
		}else{
			$data = array(
			'user_id' 			=> $tests['user_id'], 			
			'service_names'  	=> rtrim($service_names,'***'),  	
			'dressname'  		=>'', 
			'dressdescription'  =>'', 	
			'phone' 		 	=>$tests['phone'],   
			'alterphone'	 	=>$tests['alterPhone'], 				
			'landmark'	 		=>$tests['landmark'], 				
			'address'	 		=>$tests['address'],  				
			'pincode'	 		=>$tests['pincode'], 				
			'selectinsurence'	=>$tests['insurenceyes'], 				
			'insurance_id'	 	=>$tests['insurance'], 				
			'policynumber'	 	=>$tests['pnumber'], 				
			'policyholdername'	=>$tests['policyhname'], 				
			'ustdependent'	 	=>$tests['dependent'], 				
			'status'	 		=> 0,
			'added_date' 		=> date('Y-m-d H:i:s'),
			'updated_date' 		=> date('Y-m-d H:i:s'),
			);
		}
		//echo '<pre>'; print_r($data); exit;
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
	}
	public function getTests($user_id)
    {
	    $select = $this->tableGateway->getSql()->select();
		$select->join('insurances', new Expression('insurances.insurance_id=request_services.insurance_id'),array('insurance_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=request_services.user_id'),array('landmark','phone'),'left');	
		$select->where('request_services.user_id="'.$user_id.'"');
		$select->order('request_services.request_services_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	
	public function getreqserorder($req_ser_id)
    {
	    $select = $this->tableGateway->getSql()->select();
		$select->where('request_services_id="'.$req_ser_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;
	}
}